//
//  SearchResultTableViewCell.m
//  AppointmentForCar
//
//  Created by ZengPengYuan on 2016/11/14.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "SearchResultTableViewCell.h"
#import "CommonAddressModel.h"

@implementation SearchResultTableViewCell {
    UILabel *_nameLabel;
    UILabel * _addressLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.font = kAppFontSize(13);
        _addressLabel.textColor = kColorFontGray;
        _addressLabel.numberOfLines = 0;
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = kAppFontSize(14);
        _nameLabel.textColor = [UIColor blackColor];
        self.contentView.addSubview(_nameLabel).addSubview(_addressLabel);
        __weak typeof(self) weakSelf = self;

        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(15);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-15);
            make.top.equalTo(weakSelf.mas_top).mas_offset(10);
        }];
        [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom).mas_offset(12);
            make.left.right.mas_equalTo(_nameLabel);
            make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-10);
        }];
    }
    return self;
}
- (void)setModel:(BMKPoiInfo *)model
{
    _model = model;
    _addressLabel.text = model.address;
    _nameLabel.text = model.name;
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [[UIColor colorWithString:@"#cccccc120"] setStroke];
    CGContextSetLineWidth(ref, 1);

    CGContextBeginPath(ref);
    CGContextMoveToPoint(ref, rect.size.width * 0.08f, rect.size.height);
    CGContextAddLineToPoint(ref, rect.size.width, rect.size.height);
    CGContextStrokePath(ref);
}
@end


@implementation CommonResultTableViewCell
{
    CommonResultItem * _homeItem;
    CommonResultItem * _companyItem;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _homeItem = [[CommonResultItem alloc] init];
        _companyItem = [[CommonResultItem alloc] init];
        self.addSubview(_homeItem).addSubview(_companyItem);
        __weak typeof(self) weakSelf = self;
        [_homeItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.equalTo(weakSelf);
            make.width.equalTo(_companyItem.mas_width);
        }];
        [_companyItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(weakSelf);
            make.left.equalTo(_homeItem.mas_right);
        }];
        _companyItem.imageName = @"address_compan";
        _homeItem.imageName = @"address_home";
        _companyItem.title = @"公司";
        _homeItem.title = @"家";
    }
    return self;
}
- (void)setModel:(CommonAddressModel *)model
{
    _model = model;
    _companyItem.content = model.companyAddress;
    _homeItem.content = model.homeAddress;
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [[UIColor colorWithString:@"#cccccc120"] setStroke];
    CGContextSetLineWidth(ref, 1);
    CGContextBeginPath(ref);
    CGContextMoveToPoint(ref, rect.size.width / 2, 0);
    CGContextAddLineToPoint(ref, rect.size.width / 2, rect.size.height);
    CGContextStrokePath(ref);
    CGContextBeginPath(ref);
    CGContextMoveToPoint(ref, 0, rect.size.height);
    CGContextAddLineToPoint(ref, rect.size.width, rect.size.height);
    CGContextStrokePath(ref);
}
@end

@implementation CommonResultItem
{
    UIImageView * _image;
    UILabel * _titleLabel;
    UILabel * _contentLabel;
    UITapGestureRecognizer *_singleTap;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        _image = [[UIImageView alloc] initWithImage:nil];
        _titleLabel = [[UILabel alloc] init];
        _contentLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _image.contentMode = UIViewContentModeScaleToFill;
        _contentLabel.textColor = [UIColor colorWithString:@"#cccccc"];
        _titleLabel.font = kAppFontSize(12);
        _contentLabel.font = kAppFontSize(13);
        _contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _contentLabel.numberOfLines = 3;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        self.addSubview(_image).addSubview(_titleLabel).addSubview(_contentLabel);
        __weak typeof(self) weakSelf = self;
        [_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(22);
            make.width.mas_offset(18);
            make.top.equalTo(weakSelf.mas_top).mas_offset(20);
            make.left.equalTo(weakSelf.mas_left).mas_offset(5);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(_image);
            make.left.equalTo(_image.mas_right).mas_offset(kScaleWidth(5));
            make.width.mas_equalTo(kScaleWidth(30));
        }];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf).mas_offset(2);
            make.left.equalTo(_titleLabel.mas_right);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(10));
        }];
        
        _singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:_singleTap];
        
    }
    return self;
}
- (void)setContent:(NSString *)content
{
    _content = content;
    _contentLabel.text = content;
}
- (void)setImageName:(NSString *)imageName
{
    _imageName = imageName;
    _image.image = [UIImage imageNamed:imageName];
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    _titleLabel.text = title;
}

- (void)tap:(UITapGestureRecognizer *)sender {
    if (_content.length) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addressNotice" object:nil userInfo:@{@"address":_content}];
    }
}


@end
